package eu.codlab.twitteirb.manage.url;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.util.Log;

public class LoadURL {
	private String _resultat = "";
	private String _url;
	private String _location;
	private  ArrayList<String> _cookies_defined;
	public String getLocation(){
		return _location;
	}
	public LoadURL(String url) {
		_url = url;
		_cookies_defined = new ArrayList<String>();
	}

	public void log(String u1, String u2){
		//Log.d(u1,u2);
	}
	public ArrayList<String> getCookiesDefined(){
		return _cookies_defined;
	}

	public ByteArrayOutputStream loadGetUrlByte(final String[] _headers) {
		ByteArrayOutputStream ostream = new ByteArrayOutputStream();
		HttpGet httppost = new HttpGet(_url);
		log(" "," ");
		log("appel",_url);
		if (_headers != null) {
			for (int i = 0; i < _headers.length; i += 2){
				log(_headers[i], _headers[i + 1]);
				httppost.addHeader(_headers[i], _headers[i + 1]);
			}
		}
		//httppost.addHeader("Cookie", cookie_spec);

		try {
			HttpResponse response = Connexion.getConnexion().execute(
					httppost);
			status = response.getStatusLine().getStatusCode();
			Header[] _header = response.getAllHeaders();

			for (int i = 0; i < _header.length; i++) {
				log("Header receive", _header[i].getName()+" "+_header[i].getValue());
				if (_header[i].getName().indexOf("ookie") >= 0) {
					_cookies_defined.add(_header[i].getValue());
				}else if(_header[i].getName().indexOf("ocation") >= 0){
					_location=_header[i].getValue();
				}
			}
			response.getEntity().writeTo(ostream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ostream;
	}
	public String loadGetUrl(final String[] _headers) {
		return loadGetUrlByte(_headers).toString();
	}


	private int status;
	public int getStatus(){
		return status;
	}

	public String loadPostUrl(final String[] _headers,
			final List<BasicNameValuePair> _list) {
		HttpPost httppost = new HttpPost(_url);

		if (_headers != null) {
			for (int i = 0; i < _headers.length; i += 2)
				httppost.addHeader(_headers[i], _headers[i + 1]);
		}
		//httppost.addHeader("Cookie", cookie_spec);

		try {
			UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(
					_list, HTTP.UTF_8);
			httppost.setEntity(p_entity);
			//Log.d("content",""+p_entity.getContentLength());
			HttpResponse response = Connexion.getConnexion().execute(
					httppost);
			status = response.getStatusLine().getStatusCode();
			Header[] _header = response.getAllHeaders();

			for (int i = 0; i < _header.length; i++) {
				//Log.d("Header receive", _header[i].getName()+" "+_header[i].getValue());
				if (_header[i].getName().indexOf("ookie") >= 0) {
					_cookies_defined.add(_header[i].getValue());
				}else if(_header[i].getName().indexOf("ocation") >= 0){
					_location=_header[i].getValue();
				}
			}

			if (status == HttpStatus.SC_OK) {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(response.getEntity()
								.getContent()));
				StringBuffer s = new StringBuffer("");
				String l = "";
				while ((l = reader.readLine()) != null) {
					// Log.d(" ",l);
					s.append(l);
				}
				_resultat += s.toString();
			} else {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				response.getEntity().writeTo(ostream);
				_resultat += ostream.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _resultat;
	}

}
